<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>NPM | 星月前端博客</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/favicon.ico">
    <meta name="description" content="The top of the mountain is not the end, nor is the other mountain.">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.f057962c.css" as="style"><link rel="preload" href="/assets/js/app.b0d6e6c9.js" as="script"><link rel="preload" href="/assets/js/3.3f705cf6.js" as="script"><link rel="preload" href="/assets/js/1.5432b32a.js" as="script"><link rel="preload" href="/assets/js/50.8ec4f4f5.js" as="script"><link rel="prefetch" href="/assets/js/10.a8f43ca8.js"><link rel="prefetch" href="/assets/js/11.f3a618ea.js"><link rel="prefetch" href="/assets/js/12.b0d83b4c.js"><link rel="prefetch" href="/assets/js/13.f1fb4696.js"><link rel="prefetch" href="/assets/js/14.e5bc98a2.js"><link rel="prefetch" href="/assets/js/15.50519e37.js"><link rel="prefetch" href="/assets/js/16.3c6a720c.js"><link rel="prefetch" href="/assets/js/17.39fb3a22.js"><link rel="prefetch" href="/assets/js/18.76eb3752.js"><link rel="prefetch" href="/assets/js/19.7e9cf824.js"><link rel="prefetch" href="/assets/js/20.91bdeca4.js"><link rel="prefetch" href="/assets/js/21.136cc362.js"><link rel="prefetch" href="/assets/js/22.ec4f40f8.js"><link rel="prefetch" href="/assets/js/23.50127d3e.js"><link rel="prefetch" href="/assets/js/24.793b4c66.js"><link rel="prefetch" href="/assets/js/25.e2402b1b.js"><link rel="prefetch" href="/assets/js/26.cda4008e.js"><link rel="prefetch" href="/assets/js/27.8f1f302e.js"><link rel="prefetch" href="/assets/js/28.15cd65f2.js"><link rel="prefetch" href="/assets/js/29.00adce9e.js"><link rel="prefetch" href="/assets/js/30.c29d2b5d.js"><link rel="prefetch" href="/assets/js/31.40db97f7.js"><link rel="prefetch" href="/assets/js/32.70ed39cf.js"><link rel="prefetch" href="/assets/js/33.b0f7cfc3.js"><link rel="prefetch" href="/assets/js/34.f0a63370.js"><link rel="prefetch" href="/assets/js/35.88e20dad.js"><link rel="prefetch" href="/assets/js/36.13f74559.js"><link rel="prefetch" href="/assets/js/37.1d442fc4.js"><link rel="prefetch" href="/assets/js/38.84077197.js"><link rel="prefetch" href="/assets/js/39.51512938.js"><link rel="prefetch" href="/assets/js/4.3ce47df9.js"><link rel="prefetch" href="/assets/js/40.1f3d0e52.js"><link rel="prefetch" href="/assets/js/41.8e4ee580.js"><link rel="prefetch" href="/assets/js/42.690cbafe.js"><link rel="prefetch" href="/assets/js/43.c4aa2221.js"><link rel="prefetch" href="/assets/js/44.fb378084.js"><link rel="prefetch" href="/assets/js/45.77700693.js"><link rel="prefetch" href="/assets/js/46.d507cb48.js"><link rel="prefetch" href="/assets/js/47.e3a1008a.js"><link rel="prefetch" href="/assets/js/48.d6c9c78e.js"><link rel="prefetch" href="/assets/js/49.4667e7c0.js"><link rel="prefetch" href="/assets/js/5.6d641d4f.js"><link rel="prefetch" href="/assets/js/51.d9305c51.js"><link rel="prefetch" href="/assets/js/52.db11222b.js"><link rel="prefetch" href="/assets/js/53.61d62d9d.js"><link rel="prefetch" href="/assets/js/54.f20b6c65.js"><link rel="prefetch" href="/assets/js/55.0a4f147e.js"><link rel="prefetch" href="/assets/js/56.cd32d35a.js"><link rel="prefetch" href="/assets/js/57.a5fde1bb.js"><link rel="prefetch" href="/assets/js/58.a0a97490.js"><link rel="prefetch" href="/assets/js/59.1bb4fab1.js"><link rel="prefetch" href="/assets/js/6.5f336290.js"><link rel="prefetch" href="/assets/js/60.e6987c2a.js"><link rel="prefetch" href="/assets/js/61.419fcdc8.js"><link rel="prefetch" href="/assets/js/62.5a9d4e12.js"><link rel="prefetch" href="/assets/js/63.be6ca5ce.js"><link rel="prefetch" href="/assets/js/64.8aef8f94.js"><link rel="prefetch" href="/assets/js/65.16450833.js"><link rel="prefetch" href="/assets/js/66.2969e029.js"><link rel="prefetch" href="/assets/js/67.e3a3c472.js"><link rel="prefetch" href="/assets/js/68.6ff65261.js"><link rel="prefetch" href="/assets/js/69.cf4da88b.js"><link rel="prefetch" href="/assets/js/7.6a1d4e29.js"><link rel="prefetch" href="/assets/js/70.1472620a.js"><link rel="prefetch" href="/assets/js/71.1e7cb0a1.js"><link rel="prefetch" href="/assets/js/72.2bf85313.js"><link rel="prefetch" href="/assets/js/73.d9cb4d96.js"><link rel="prefetch" href="/assets/js/74.0c536977.js"><link rel="prefetch" href="/assets/js/75.4f82c6a8.js"><link rel="prefetch" href="/assets/js/76.42f3f0b7.js"><link rel="prefetch" href="/assets/js/77.70961c41.js"><link rel="prefetch" href="/assets/js/78.2f45f2fc.js"><link rel="prefetch" href="/assets/js/79.a68a9b1c.js"><link rel="prefetch" href="/assets/js/8.378e22b1.js"><link rel="prefetch" href="/assets/js/80.6cb339b8.js"><link rel="prefetch" href="/assets/js/81.5849f124.js"><link rel="prefetch" href="/assets/js/82.f5531d2d.js"><link rel="prefetch" href="/assets/js/83.47badffa.js"><link rel="prefetch" href="/assets/js/84.dece7e94.js"><link rel="prefetch" href="/assets/js/85.d9e7f122.js"><link rel="prefetch" href="/assets/js/86.233d7088.js"><link rel="prefetch" href="/assets/js/87.ce5dff2e.js"><link rel="prefetch" href="/assets/js/88.f976bef7.js"><link rel="prefetch" href="/assets/js/89.8473e540.js"><link rel="prefetch" href="/assets/js/9.9eb4a88c.js"><link rel="prefetch" href="/assets/js/90.d6288b36.js"><link rel="prefetch" href="/assets/js/91.989f4dea.js"><link rel="prefetch" href="/assets/js/92.6ff507f9.js">
    <link rel="stylesheet" href="/assets/css/0.styles.f057962c.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-5bb33761><div data-v-5bb33761><div class="password-shadow password-wrapper-out" style="display:none;" data-v-59e6cb88 data-v-5bb33761 data-v-5bb33761><h3 class="title" data-v-59e6cb88>星月前端博客</h3> <p class="description" data-v-59e6cb88>The top of the mountain is not the end, nor is the other mountain.</p> <label id="box" class="inputBox" data-v-59e6cb88><input type="password" value="" data-v-59e6cb88> <span data-v-59e6cb88>Konck! Knock!</span> <button data-v-59e6cb88>OK</button></label> <div class="footer" data-v-59e6cb88><span data-v-59e6cb88><i class="iconfont reco-theme" data-v-59e6cb88></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-59e6cb88>vuePress-theme-reco</a></span> <span data-v-59e6cb88><i class="iconfont reco-copyright" data-v-59e6cb88></i> <a data-v-59e6cb88><span data-v-59e6cb88>星月</span>
          
        <!---->
        2022
      </a></span></div></div> <div class="hide" data-v-5bb33761><header class="navbar" data-v-5bb33761><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="星月前端博客" class="logo"> <span class="site-name">星月前端博客</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      博客
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/ES6模块化与异步编程/" class="nav-link"><i class="undefined"></i>
  ES6模块化与异步编程
</a></li><li class="dropdown-item"><!----> <a href="/categories/css/" class="nav-link"><i class="undefined"></i>
  css
</a></li><li class="dropdown-item"><!----> <a href="/categories/TypeScript/" class="nav-link"><i class="undefined"></i>
  TypeScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/html/" class="nav-link"><i class="undefined"></i>
  html
</a></li><li class="dropdown-item"><!----> <a href="/categories/JavaScript/" class="nav-link"><i class="undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/Node.js/" class="nav-link"><i class="undefined"></i>
  Node.js
</a></li><li class="dropdown-item"><!----> <a href="/categories/Vue2/" class="nav-link"><i class="undefined"></i>
  Vue2
</a></li><li class="dropdown-item"><!----> <a href="/categories/Vue3/" class="nav-link"><i class="undefined"></i>
  Vue3
</a></li><li class="dropdown-item"><!----> <a href="/categories/webpack/" class="nav-link"><i class="undefined"></i>
  webpack
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="/docs/theme-reco/" class="nav-link"><i class="iconfont reco-message"></i>
  随记
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系方式
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/858399075" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/xingyueqianduan" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="http://blog.yhxweb.top/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i></i>
  QQ:76120220
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="http://blog.yhxweb.top/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i></i>
  WX:15237156071
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-5bb33761></div> <aside class="sidebar" data-v-5bb33761><div class="personal-info-wrapper" data-v-1fad0c41 data-v-5bb33761><img src="/avatar.png" alt="author-avatar" class="personal-img" data-v-1fad0c41> <h3 class="name" data-v-1fad0c41>
    星月
  </h3> <div class="num" data-v-1fad0c41><div data-v-1fad0c41><h3 data-v-1fad0c41>82</h3> <h6 data-v-1fad0c41>Articles</h6></div> <div data-v-1fad0c41><h3 data-v-1fad0c41>9</h3> <h6 data-v-1fad0c41>Tags</h6></div></div> <ul class="social-links" data-v-1fad0c41></ul> <hr data-v-1fad0c41></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      博客
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/ES6模块化与异步编程/" class="nav-link"><i class="undefined"></i>
  ES6模块化与异步编程
</a></li><li class="dropdown-item"><!----> <a href="/categories/css/" class="nav-link"><i class="undefined"></i>
  css
</a></li><li class="dropdown-item"><!----> <a href="/categories/TypeScript/" class="nav-link"><i class="undefined"></i>
  TypeScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/html/" class="nav-link"><i class="undefined"></i>
  html
</a></li><li class="dropdown-item"><!----> <a href="/categories/JavaScript/" class="nav-link"><i class="undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/Node.js/" class="nav-link"><i class="undefined"></i>
  Node.js
</a></li><li class="dropdown-item"><!----> <a href="/categories/Vue2/" class="nav-link"><i class="undefined"></i>
  Vue2
</a></li><li class="dropdown-item"><!----> <a href="/categories/Vue3/" class="nav-link"><i class="undefined"></i>
  Vue3
</a></li><li class="dropdown-item"><!----> <a href="/categories/webpack/" class="nav-link"><i class="undefined"></i>
  webpack
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="/docs/theme-reco/" class="nav-link"><i class="iconfont reco-message"></i>
  随记
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系方式
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/858399075" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/xingyueqianduan" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="http://blog.yhxweb.top/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i></i>
  QQ:76120220
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="http://blog.yhxweb.top/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i></i>
  WX:15237156071
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-59e6cb88 data-v-5bb33761><h3 class="title" data-v-59e6cb88>NPM</h3> <!----> <label id="box" class="inputBox" data-v-59e6cb88><input type="password" value="" data-v-59e6cb88> <span data-v-59e6cb88>Konck! Knock!</span> <button data-v-59e6cb88>OK</button></label> <div class="footer" data-v-59e6cb88><span data-v-59e6cb88><i class="iconfont reco-theme" data-v-59e6cb88></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-59e6cb88>vuePress-theme-reco</a></span> <span data-v-59e6cb88><i class="iconfont reco-copyright" data-v-59e6cb88></i> <a data-v-59e6cb88><span data-v-59e6cb88>星月</span>
          
        <!---->
        2022
      </a></span></div></div> <div data-v-5bb33761><div data-v-5bb33761><main class="page"><section style="display:;"><div class="page-title"><h1 class="title">NPM</h1> <div data-v-8a445198><i class="iconfont reco-account" data-v-8a445198><span data-v-8a445198>星月</span></i> <i class="iconfont reco-date" data-v-8a445198><span data-v-8a445198>5/18/2022</span></i> <!----> <i class="tags iconfont reco-tag" data-v-8a445198><span class="tag-item" data-v-8a445198>node.js</span></i></div></div> <div class="theme-reco-content content__default"><h1 id="npm"><a href="#npm" class="header-anchor">#</a> NPM</h1> <h2 id="npmrc"><a href="#npmrc" class="header-anchor">#</a> .npmrc</h2> <p>NPM的配置文件</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token assign-left variable">registry</span><span class="token operator">=</span>https://registry.npmjs.org/
package-lock<span class="token operator">=</span>false <span class="token comment"># 不启用NPM锁</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="命令"><a href="#命令" class="header-anchor">#</a> 命令</h2> <h3 id="npm-config"><a href="#npm-config" class="header-anchor">#</a> npm config</h3> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> config <span class="token builtin class-name">set</span> registry https://registry.npmjs.org/
<span class="token function">npm</span> config get prefix 
<span class="token function">npm</span> config delete registry
<span class="token function">npm</span> config 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="npm-init"><a href="#npm-init" class="header-anchor">#</a> npm init</h3> <p>初始化<code>package.json</code></p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> init -y
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="npm-install"><a href="#npm-install" class="header-anchor">#</a> npm install</h3> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> <span class="token function">install</span> <span class="token operator">&lt;</span>name<span class="token operator">&gt;</span> --save <span class="token comment"># yarn add &lt;name&gt;</span>
<span class="token function">npm</span> <span class="token function">install</span> <span class="token operator">&lt;</span>name<span class="token operator">&gt;</span> --save-dev <span class="token comment"># yarn add &lt;name&gt; -D</span>
<span class="token function">npm</span> <span class="token function">install</span> -g <span class="token operator">&lt;</span>name<span class="token operator">&gt;</span> <span class="token comment"># yarn global add &lt;name&gt;</span>

<span class="token function">npm</span> <span class="token function">install</span>
<span class="token function">npm</span> ci <span class="token comment"># 通常用于CI，使用该命令时需要确保项目中存在 package-lock.json或 npm-shrinkwrap.json，并且当 package.json和 package-lock.json中依赖的版本不一致时 npm ci会抛出错误。</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>本地安装时，模块会被安装在<code>/project/node_modules</code>下，同时如果该模块的<code>package.json</code>中存在<code>bin</code>字段时，则会自动根据<code>bin</code>表示的字典在<code>/project/node_modules/.bin</code>下面创建对应的符号链接。</p> <p>全局安装时，模块会被安装在<code>/usr/local/lib/node_modules</code>（以MacOS举例）下，同时如果该模块的<code>package.json</code>中存在<code>bin</code>字段时，则会自动根据<code>bin</code>表示的字典在<code>/usr/local/bin</code>下面创建对应的符号链接（可理解为Windows中的快捷方式）。</p> <p>举个例子，<code>akara-project</code>的<code>package.json</code>中的<code>bin</code>字段如下，那么当全局安装<code>akara-project</code>时会创建<code>/usr/local/bin/akara</code>这个文件（符号链接），这个文件实际指向着<code>akara-project</code>根路径下的<code>index.js</code>。（其实这就是开发命令行工具的原理）</p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
  <span class="token property">&quot;bin&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;akara&quot;</span><span class="token operator">:</span> <span class="token string">&quot;index.js&quot;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="npm-uninstall"><a href="#npm-uninstall" class="header-anchor">#</a> npm uninstall</h3> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> uninstall <span class="token operator">&lt;</span>name<span class="token operator">&gt;</span> <span class="token comment"># yarn remove &lt;name&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="npm-update"><a href="#npm-update" class="header-anchor">#</a> npm update</h3> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> update <span class="token operator">&lt;</span>name<span class="token operator">&gt;</span> <span class="token comment"># yarn upgrade &lt;name&gt;</span>

<span class="token function">npm</span> update
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>举个例子，如果我们的项目（采用锁机制）存在老版本的<code>react@^16.0.0</code>，此时我们（包括CI）只能拿到<code>16.0.0</code>版本的<code>react</code>。如果我们现在想要升级<code>react</code>可以采取两种方式：<code>npm install react</code>或<code>npm update react</code>。前者更类似于重新安装<code>react</code>，并会重写<code>package.json</code>中的依赖关系；而<code>npm update</code>并不会改动<code>package.json</code>，它仅仅是根据<code>package.json</code>中的版本<code>semver</code>来重新安装可安装的最新<code>react</code>。二者都会重新生成<code>package-lock.json</code>。</p> <h3 id="npm-outdated"><a href="#npm-outdated" class="header-anchor">#</a> npm outdated</h3> <p>查看项目中哪些模块不是最新版本</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> outdated 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="npm-run"><a href="#npm-run" class="header-anchor">#</a> npm run</h3> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> run start <span class="token comment"># 执行脚本</span>
<span class="token function">npm</span> run-script <span class="token operator">&lt;</span>stage<span class="token operator">&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="npm-link"><a href="#npm-link" class="header-anchor">#</a> npm link</h3> <blockquote><p>alias: npm ln</p></blockquote> <p>简单来说这个命令存在两种用法，可用于将本地模块链接到全局，或是将全局模块链接到本地。</p> <p>用法一：</p> <p>当我们位于模块<code>akara-project</code>项目中时，直接执行<code>npm link</code>会把当前模块链接到全局（具体来说的话，会在<code>/usr/local/lib/node_modules</code>下创建一个符号链接指向着<code>akara-project</code>这个模块，如果该模块的<code>package.json</code>存在<code>bin</code>字段时还会在<code>/usr/local/bin</code>下面创建对应的符号链接）</p> <p>用法二：</p> <p>当我们位于某个项目中时，并假设我们已经全局安装了一个模块（如<code>pm2</code>），此时在项目中执行<code>npm link webpack</code>会把全局模块链接到本地（具体来说的话，会在当前项目的<code>node_modules</code>创建一个符号链接<code>pm2</code>指向着全局的<code>pm2</code>模块，如果该模块的<code>package.json</code>存在<code>bin</code>字段时还会在<code>/project/node_modules/.bin</code>下面创建对应的符号链接）</p> <p>通过结合方法一和方法二我们可以实现这样的功能：假设我们同时维护着项目A和模块B，并在项目A中引用着模块B。那么比较传统的方法来维护这两个库是这样的，更新完模块B后发布，然后在项目A中更新模块B从而查看最新的效果；而通过<code>npm link</code>我们可以简化这个流程，我们只需要先在B模块中通过<code>npm link</code>来把B模块链接到全局，然后在A项目中通过<code>npm link B</code>来把全局B模块链接到A项目本地。</p> <h3 id="npm-exec"><a href="#npm-exec" class="header-anchor">#</a> npm exec</h3> <p>可以直接执行<code>node_modules/.bin</code>下的可执行文件</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> <span class="token builtin class-name">exec</span> webpack <span class="token comment"># 等于npx webpack</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="npm-publish"><a href="#npm-publish" class="header-anchor">#</a> npm publish</h3> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> publish <span class="token comment"># 发布模块</span>
<span class="token function">npm</span> unpublish --force <span class="token comment"># 下架模块</span>

<span class="token function">npm</span> publish --access publish <span class="token comment"># 发布公共模块</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>在通过<code>npm login</code>登陆了<code>npm</code>账户后，我们可以在任意项目下通过<code>npm publish</code>来进行模块的发布，此时NPM镜像源需要是官方镜像源。</p> <p>除了通常的模块，我们还可以发布类似<code>@akara/my-package</code>这样的作用域模块，此时我们需要<code>npm publish --access publish</code>。这是因为NPM模块分为公共模块和私有模块，通常我们发布的都是公共模块，而发布私有模块是收费的，同时发布作用域模块默认是发布私有模块，因此我们需要显式的制定模块的类型。</p> <p>发布NPM模块时，<code>.gitignore</code>和<code>.npmignore</code>中指定的文件不会被发布出去。除此之外我们还可以通过<code>package.json</code>的<code>files</code>来指定只有哪些文件能被发布。</p> <ol><li><code>.gitignore</code>中的文件不会被发布</li> <li><code>.npmignore</code>中的文件不会被发布</li> <li><code>package.json</code>中的 <code>files</code>字段指定哪些文件会被发布</li></ol> <h3 id="npm-view"><a href="#npm-view" class="header-anchor">#</a> npm view</h3> <p>查看一个模块的信息</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> view <span class="token operator">&lt;</span>name<span class="token operator">&gt;</span> <span class="token comment"># e.g npm view antd</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="npm-version"><a href="#npm-version" class="header-anchor">#</a> npm version</h3> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> version <span class="token comment"># 查看当前版本</span>
<span class="token function">npm</span> version patch <span class="token comment"># 升级一个补丁版本，同时自动git commit并打上版本号对应的git tag，如v1.0.1</span>
<span class="token function">npm</span> version minor <span class="token comment"># 升级一个小版本，如v1.1.1</span>
<span class="token function">npm</span> version major <span class="token comment"># 升级一个大版本，如v2.0.0</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="npm-audit"><a href="#npm-audit" class="header-anchor">#</a> npm audit</h3> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> audit <span class="token comment"># 查看当前项目所有依赖模块的漏洞</span>
<span class="token function">npm</span> audit --fix <span class="token comment"># 更新所有存在漏洞的模块来修复漏洞</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>当我们安装模块时会自动提示当前版本的模块的漏洞，我们也可以通过该命令来查找当前项目所有依赖中可能存在的漏洞</p> <h3 id="npm-fund"><a href="#npm-fund" class="header-anchor">#</a> npm fund</h3> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> fund
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>当我们安装模块时会自动提示有多少个模块正在寻找投资/资助，我们也可以通过该命令来查看具体是哪些命令在寻找投资</p> <h2 id="package-json"><a href="#package-json" class="header-anchor">#</a> package.json</h2> <h3 id="type"><a href="#type" class="header-anchor">#</a> type</h3> <p>以<code>.cjs</code>结尾的文件会被视为<code>CommonJS</code>模块，以<code>.mjs</code>结尾的文件会被视为<code>ES</code>模块，而普通的<code>.js</code>文件则会根据<code>type</code>字段的不同视为不同的模块，默认不带<code>type</code>视为<code>CommonJS</code>模块，<code>type: 'module'</code>则视为<code>ES</code>模块。</p> <h3 id="files"><a href="#files" class="header-anchor">#</a> files</h3> <p>对于一些模块而言，用户只需要引用该模块源码构建后的产物，而并不想连源代码也一起安装。这样的模块通常会通过<code>files</code>字段来规定哪些模块才会被发布，如<code>&quot;file&quot;: [&quot;dist&quot;, &quot;README.md&quot;]</code></p> <p>需要注意的是以下文件永远都会被外部下载：<code>package.json</code>、<code>README</code>、<code>CHANGE / CHANGELOG / HISTORY</code>、<code>LICENSE / LICENCE</code>、<code>NOTICE</code>、<code>main</code>字段指向的文件。</p> <h3 id="main"><a href="#main" class="header-anchor">#</a> main</h3> <p>用来规定模块的默认入口，默认值为 <code>index.js</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> test <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'my-module'</span><span class="token punctuation">)</span> <span class="token comment">// 引入my-module模块的根目录的index.js文件</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="exports"><a href="#exports" class="header-anchor">#</a> exports</h3> <p>功能类似<code>main</code>，同时存在<code>exports</code>和<code>main</code>时，<code>exports</code>字段的优先级更高</p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;exports&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">&quot;.&quot;</span><span class="token operator">:</span> <span class="token string">&quot;./index.js&quot;</span><span class="token punctuation">,</span> 
        <span class="token property">&quot;./test&quot;</span><span class="token operator">:</span> <span class="token string">&quot;./src/test.js&quot;</span> 
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> test <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'my-module'</span><span class="token punctuation">)</span> <span class="token comment">// ./index.js</span>
<span class="token keyword">const</span> test2 <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'my-module/test'</span><span class="token punctuation">)</span> <span class="token comment">// ./src/test.js</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>可以看到一旦使用了<code>exports</code>字段，那么模块的引用规则就和以往有较大的区别，此时我们不再能根据模块的任意路径来引用相对应的文件，只能根据<code>exports</code>所指定的映射关系来引用所给定的文件。</p> <p><code>exports</code>还能够根据导入模块时使用的是 <code>require</code>还是 <code>import</code>选择不同的导出。</p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;exports&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">&quot;.&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
            <span class="token property">&quot;require&quot;</span><span class="token operator">:</span> <span class="token string">&quot;./a.js&quot;</span><span class="token punctuation">,</span>
            <span class="token property">&quot;import&quot;</span><span class="token operator">:</span> <span class="token string">&quot;./b.mjs&quot;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="bin"><a href="#bin" class="header-anchor">#</a> bin</h3> <p>如之前说过那样，当我们通过<code>npm install</code>安装模块时，会根据该模块<code>package.json</code>中的<code>bin</code>字段来创建指向指定文件的符号链接</p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
	<span class="token property">&quot;bin&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;akara&quot;</span><span class="token operator">:</span> <span class="token string">&quot;index.js&quot;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>假设<code>akara-project</code>的配置如上，那么全局安装<code>akara-project</code>时会创建<code>/usr/local/bin/akara</code>符号链接（指向着<code>/usr/local/lib/node_modules/akara-project/index.js</code>），所以现在我们可以直接在命令行中输入<code>akara</code>来执行对应的<code>index.js</code>（当然实际上我们还需要两个小步骤，一是我们需要规定<code>index.js</code>这个文件执行的环境，因此需要在<code>index.js</code>代码的第一行加上<code>#!/usr/bin/env node</code>；二是我们需要通过执行<code>chmod +x index.js</code>来给予该文件可执行权限）</p> <p>如果我们是本地安装而不是全局安装<code>akara-project</code>，那么创建的符号链接<code>akara</code>会被放在<code>project/node_modules/.bin</code>下面，此时无法直接通过在命令行输入<code>akara</code>来执行命令，我们有其他的几种方式</p> <ol><li><p><code>./node_modules/.bin/akara</code></p></li> <li><p><code>package.json</code>的 <code>script</code>字段中填写执行方式，如</p> <div class="language-1 line-numbers-mode"><pre class="language-text"><code>{
    &quot;script&quot;: {
        &quot;run-my-script&quot;: &quot;akara&quot;
    }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li> <li><p><code>npm exec akara</code></p></li> <li><p><code>npx akara</code></p></li></ol> <h3 id="script"><a href="#script" class="header-anchor">#</a> script</h3> <p><code>npm</code>除了 <code>npm ci</code>、<code>npm install</code>等内置脚本，还包括 <code>hook script</code>（<code>pre/post script）</code>和 <code>lifecycle script</code>。</p> <h4 id="pre-post"><a href="#pre-post" class="header-anchor">#</a> pre &amp; Post</h4> <p>对于一个脚本我们可能想要在其执行之前或之后执行某些操作，此时可以使用 <code>pre</code>或 <code>post</code>前缀。</p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;script&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">&quot;test&quot;</span><span class="token operator">:</span> <span class="token string">&quot;echo \&quot;i'm test\&quot;&quot;</span><span class="token punctuation">,</span>
        <span class="token property">&quot;pretest&quot;</span><span class="token operator">:</span> <span class="token string">&quot;echo \&quot;在test脚本执行前执行\&quot;&quot;</span><span class="token punctuation">,</span>
        <span class="token property">&quot;posttest&quot;</span><span class="token operator">:</span> <span class="token string">&quot;echo \&quot;在test脚本执行后执行\&quot;&quot;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h4 id="lifecycle"><a href="#lifecycle" class="header-anchor">#</a> LifeCycle</h4> <p><code>npm</code>内置了一些生命周期脚本，如<code>prepare</code>、<code>prepack</code>等</p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;script&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">&quot;prepare&quot;</span><span class="token operator">:</span> <span class="token string">&quot;echo \&quot;hello akara\&quot;&quot;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="dependencies"><a href="#dependencies" class="header-anchor">#</a> dependencies</h3> <p>项目的依赖</p> <h3 id="devdependencies"><a href="#devdependencies" class="header-anchor">#</a> devDependencies</h3> <p>项目的开发依赖。如果我们在NPM发布了一个模块A，之后当我们 <code>npm install A</code>时会自动安装模块A的依赖，但不会安装模块A的开发依赖。</p> <h3 id="peerdependencies"><a href="#peerdependencies" class="header-anchor">#</a> peerDependencies</h3> <p>假设存在A库，该库存在一个插件B，那么很明显插件B自身是不依赖于库A的，但是又需要你的项目中存在库A，那么插件B的 <code>package.json</code>中就可以通过 <code>peerDependencies</code>来指定同级依赖关系。比如插件B只能在1.0版本的A库中起作用，那么 <code>peerDependencies</code>可能是 <code>A@1.0.0</code>，当你的项目中同时安装了插件B和2.0版本的A库时就会出现警告。</p> <h3 id="resolutions"><a href="#resolutions" class="header-anchor">#</a> resolutions</h3> <p>假设我们项目直接依赖<code>react-router</code>，而<code>react-router</code>内部又依赖<code>@types/react: '*'</code>。这意味着当我们安装<code>react-router</code>时会自动安装最新版本的<code>@types/react</code>，又已知最新版本的<code>@types/react</code>（比如<code>v18</code>）引入了一些破坏性变更，我们此时希望能够安装更低版本的<code>@types/react</code>来避免错误提示，这个时候可以给<code>package.json</code>添加<code>resolutions</code>字段，并通过<code>yarn install</code>重新安装。<a href="https://classic.yarnpkg.com/lang/en/docs/selective-version-resolutions/" target="_blank" rel="noopener noreferrer">Selective dependency resolutions<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
  resolutions<span class="token operator">:</span> <span class="token punctuation">{</span>
    '@types/react'<span class="token operator">:</span> '^<span class="token number">17.0</span>.<span class="token number">0</span>'
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="browser-module"><a href="#browser-module" class="header-anchor">#</a> browser | module</h3> <p>一般 <code>browser</code>字段指向 <code>cjs</code>或 <code>umd</code>模块，<code>module</code>字段指向 <code>es</code>模块，大多数情况下这两个字段都没什么用处。</p> <p>在某些情况下，特别是使用 <code>webpack</code>打包模块时，当 <code>webpack</code>配置的 <code>target</code>为 <code>web</code>（默认值），会根据模块的 <code>browser</code>字段导入模块；通过设置 <code>target</code>为 <code>node</code>，会根据 <code>module</code>字段导入模块。</p> <p>另外，当 <code>package.json</code>不存在对应的入口字段，会根据 <code>browser -&gt; module -&gt; main</code>的优先级导入模块。这个优先级是根据配置 <code>resolve.mainFields</code>字段指定的，我们可以通过修改该字段来调整优先级：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// webpack.config.js</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token literal-property property">target</span><span class="token operator">:</span> <span class="token string">'node'</span><span class="token punctuation">,</span> <span class="token comment">// 默认值web</span>
    <span class="token literal-property property">resolve</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token literal-property property">mainFields</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'main'</span><span class="token punctuation">,</span> <span class="token string">'module'</span><span class="token punctuation">,</span> <span class="token string">'browser'</span><span class="token punctuation">]</span> <span class="token comment">// 默认值 ['browser', 'module', 'main']</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h2 id="package-lock-json"><a href="#package-lock-json" class="header-anchor">#</a> package-lock.json</h2> <p>在 <code>package.json</code>的 <code>dependencies</code>字段中我们经常能看见这种形式的版本号 <code>&quot;react&quot;: &quot;^17.0.2&quot;</code>、<code>&quot;xx&quot;: &quot;~0.10.0&quot;</code>，这种写法通常被称为<a href="https://github.com/npm/node-semver" target="_blank" rel="noopener noreferrer"><code>semver</code>表示法<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，三个数字分别表示主要版本、次要版本、补丁版本。</p> <p>当我们使用 <code>npm install &lt;name&gt;</code>来安装依赖，或者是在已有的项目中使用 <code>npm update</code>来更新依赖，都会根据 <code>semver</code>规则安装对应版本的模块，也就是说<strong>实际安装版本并不是固定的</strong>。</p> <ul><li><code>^</code>：表示只会执行不更改最左边非零数字的更新。比如 <code>^0.10.0</code>，意味着我们可以安装（或更新，下同）<code>0.10.1</code>等版本，但不能安装 <code>0.11.0</code>或更高的版本；又比如 <code>^1.10.0</code>，意味着我们可以安装 <code>1.10.1</code>、<code>1.11.0</code>等版本，但不能安装 <code>2.0.0</code>或更高的版本。</li> <li><code>~</code>：如果我在比较器中指定了次要版本，那么只允许补丁版本的更新；如果没有指定次要版本，那么可以允许次要版本的更新，所以通常情况 <code>~</code>只允许补丁级别的更新。比如 <code>~1.10.0</code>的依赖，意味着我们只能安装 <code>1.10.x</code>的版本，不能安装 <code>1.11.0</code>的版本。</li></ul> <p>而这也带来了一个新的问题，对于同一个项目在不同时机安装的依赖版本可能不一致，这就带来了相当大的风险和不可控性，特别是当依赖的某个包更新了一个漏洞，那也会影响到我们新构建的代码。</p> <p>因此高版本<code>yarn</code>和<code>npm</code>都默认启用了<code>lock</code>机制，当我们<code>npm install &lt;name&gt;</code>安装依赖或<code>npm update</code>更新依赖的时候都会生成<code>package-lock.json</code>（<code>yarn</code>对应<code>yarn.lock</code>），那当其他人<code>clone</code>项目并<code>npm install</code>时则会根据<code>package-lock.json</code>来安装指定版本的模块。</p> <hr> <p>不过很明显锁不锁版本都有对应的好处和坏处，所以社区对是否锁版本还存在着一些<a href="https://www.zhihu.com/question/65536076" target="_blank" rel="noopener noreferrer">争论<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>。</p> <p>另外，无论是否使用 <code>lock</code>机制，都不应该把 <code>package-lock.json</code>写入 <code>.gitignore</code>中。</p> <p>如果我们使用 <code>lock</code>机制，应该直接把 <code>package-lock.json</code>提交进仓库；如果我们不使用 <code>lock</code>机制，则应该在 <code>.npmrc</code>中写入 <code>package-lock=false</code>来关闭 <code>lock</code>机制，并把 <code>package-lock.json</code>提交到仓库中。<a href="https://www.zhihu.com/question/264560841" target="_blank" rel="noopener noreferrer">参考<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h2 id="node-modules"><a href="#node-modules" class="header-anchor">#</a> node_modules</h2> <p>在 <code>NPM</code>的早期版本，<code>node_modules</code>使用嵌套结构来管理模块之间的依赖关系。而我们的很多模块都又可能依赖于同一个模块，这样的结构可能导致性能的浪费。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>- A -&gt; B
- C -&gt; B
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>所以在 <code>NPM@3.x</code>以后，<code>node_modules</code>主要使用扁平结构来管理模块之间的依赖关系。假设我们安装了A和C模块，这两个模块同时依赖于同一个版本的B模块，此时 <code>node_modules</code>结构如下。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>- A
- C
- B
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>不过有的时候，我们安装的模块A和C可能依赖于同一个模块B的不同版本。</p> <ol><li><p>假设 <code>node_modules</code>存在 <code>B@1.1.0</code>。然后我们安装的模块A依赖于 <code>B@^1.1.0</code>，即使模块B最新版本已经到了 <code>1.9.0</code>，我们也会复用原本已安装的模块。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>- A
- B@1.1.0
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li> <li><p>假设 <code>node_modules</code>存在 <code>B@1.1.0</code>。然后我们安装的模块A依赖于 <code>B@^1.2.0</code>，则会安装最新的模块（如 <code>B@1.9.0</code>）</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>- A -&gt; B@1.9.0 
- B@1.1.0
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li></ol></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">8/8/2022, 7:33:24 PM</span></div></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-ac050c62><li class="level-2" data-v-ac050c62><a href="/blogs/node/2022/5.html#npmrc" class="sidebar-link reco-side-npmrc" data-v-ac050c62>.npmrc</a></li><li class="level-2" data-v-ac050c62><a href="/blogs/node/2022/5.html#命令" class="sidebar-link reco-side-命令" data-v-ac050c62>命令</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#npm-config" class="sidebar-link reco-side-npm-config" data-v-ac050c62>npm config</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#npm-init" class="sidebar-link reco-side-npm-init" data-v-ac050c62>npm init</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#npm-install" class="sidebar-link reco-side-npm-install" data-v-ac050c62>npm install</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#npm-uninstall" class="sidebar-link reco-side-npm-uninstall" data-v-ac050c62>npm uninstall</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#npm-update" class="sidebar-link reco-side-npm-update" data-v-ac050c62>npm update</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#npm-outdated" class="sidebar-link reco-side-npm-outdated" data-v-ac050c62>npm outdated</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#npm-run" class="sidebar-link reco-side-npm-run" data-v-ac050c62>npm run</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#npm-link" class="sidebar-link reco-side-npm-link" data-v-ac050c62>npm link</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#npm-exec" class="sidebar-link reco-side-npm-exec" data-v-ac050c62>npm exec</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#npm-publish" class="sidebar-link reco-side-npm-publish" data-v-ac050c62>npm publish</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#npm-view" class="sidebar-link reco-side-npm-view" data-v-ac050c62>npm view</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#npm-version" class="sidebar-link reco-side-npm-version" data-v-ac050c62>npm version</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#npm-audit" class="sidebar-link reco-side-npm-audit" data-v-ac050c62>npm audit</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#npm-fund" class="sidebar-link reco-side-npm-fund" data-v-ac050c62>npm fund</a></li><li class="level-2" data-v-ac050c62><a href="/blogs/node/2022/5.html#package-json" class="sidebar-link reco-side-package-json" data-v-ac050c62>package.json</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#type" class="sidebar-link reco-side-type" data-v-ac050c62>type</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#files" class="sidebar-link reco-side-files" data-v-ac050c62>files</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#main" class="sidebar-link reco-side-main" data-v-ac050c62>main</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#exports" class="sidebar-link reco-side-exports" data-v-ac050c62>exports</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#bin" class="sidebar-link reco-side-bin" data-v-ac050c62>bin</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#script" class="sidebar-link reco-side-script" data-v-ac050c62>script</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#dependencies" class="sidebar-link reco-side-dependencies" data-v-ac050c62>dependencies</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#devdependencies" class="sidebar-link reco-side-devdependencies" data-v-ac050c62>devDependencies</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#peerdependencies" class="sidebar-link reco-side-peerdependencies" data-v-ac050c62>peerDependencies</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#resolutions" class="sidebar-link reco-side-resolutions" data-v-ac050c62>resolutions</a></li><li class="level-3" data-v-ac050c62><a href="/blogs/node/2022/5.html#browser-module" class="sidebar-link reco-side-browser-module" data-v-ac050c62>browser | module</a></li><li class="level-2" data-v-ac050c62><a href="/blogs/node/2022/5.html#package-lock-json" class="sidebar-link reco-side-package-lock-json" data-v-ac050c62>package-lock.json</a></li><li class="level-2" data-v-ac050c62><a href="/blogs/node/2022/5.html#node-modules" class="sidebar-link reco-side-node-modules" data-v-ac050c62>node_modules</a></li></ul></main></div> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><div class="kanbanniang" data-v-5775ee02><div class="banniang-container" style="display:;" data-v-5775ee02><div class="messageBox" style="right:68px;bottom:190px;display:none;" data-v-5775ee02>
      欢迎来到 星月前端博客
    </div> <div class="operation" style="right:90px;bottom:40px;display:;" data-v-5775ee02><i class="kbnfont kbn-ban-home ban-home" data-v-5775ee02></i> <i class="kbnfont kbn-ban-message message" data-v-5775ee02></i> <i class="kbnfont kbn-ban-close close" data-v-5775ee02></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-5775ee02><i class="kbnfont kbn-ban-info info" data-v-5775ee02></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:none;" data-v-5775ee02></i></div> <canvas id="banniang" width="150" height="220" class="live2d" style="right:90px;bottom:-20px;opacity:0.9;" data-v-5775ee02></canvas></div> <div class="showBanNiang" style="display:none;" data-v-5775ee02>
    看板娘
  </div></div><canvas id="vuepress-canvas-cursor"></canvas></div></div>
    <script src="/assets/js/app.b0d6e6c9.js" defer></script><script src="/assets/js/3.3f705cf6.js" defer></script><script src="/assets/js/1.5432b32a.js" defer></script><script src="/assets/js/50.8ec4f4f5.js" defer></script>
  </body>
</html>
